using System;
using System.Collections.Generic;
using Gameplay.PVE.Entity;
using UnityEngine;
using Yoozoo.Libs;

namespace Gameplay.PVE
{
    public class StageBase:IClass
    {
        void IClass.OnReset()
        {
            actionList.Clear();
            players = null;
            subPlayers = null;
        }

        public struct StageAction
        {
            public float time;
            public Action action;
        }

        protected int id;

        protected List<UnitBase> players;
        protected List<UnitBase> subPlayers;

        protected List<StageAction> actionList = new List<StageAction>();
        protected float startTime = 0;
        protected float duration = 2;

        protected bool isTimeFinished;
        protected bool isActionFinished;
        
        public virtual void Start(int id,List<UnitBase> players, List<UnitBase> subPlayers)
        {
            this.id = id;
            startTime = TimeManager.logicTime;
            isTimeFinished = false;
            isActionFinished = false;
            this.players = players;
            this.subPlayers = subPlayers;
            InitData();
        }

        protected virtual void InitData()
        {
            
        }

        public void SetDuration(float duration)
        {
            this.duration = duration;
        }

        public virtual bool IsFinish()
        {
            return isActionFinished && isTimeFinished;
        }

        public virtual void Update()
        {
            for (int i = 0; i < actionList.Count; i++)
            {
                if (TimeManager.logicTime - startTime >= actionList[i].time)
                {
                    actionList[i].action.Invoke();
                    actionList.RemoveAt(i);
                    i--;
                }
            }
            if (TimeManager.logicTime - startTime > duration)
            {
                isTimeFinished = true;
            }
        }

        public virtual void AddAction(Action specialAction,float time)
        {
            actionList.Add(new StageAction
            {
                action = specialAction,
                time = time,
            });
        }
    }
}